package com.ruoyi.web.controller.pk;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pk.domain.PkEjDailyReport;
import com.ruoyi.pk.domain.PkZlDailyReport;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pk.domain.PkEjDailyReport;
import com.ruoyi.pk.service.IPkEjDailyReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 二九日报Controller
 *
 * @author wang
 * @date 2021-09-13
 */
@RestController
@RequestMapping("/pk/ej_daily_report")
public class PkEjDailyReportController extends BaseController
{
    @Autowired
    private IPkEjDailyReportService pkEjDailyReportService;
    @Autowired
    private SysUserMapper userMapper;
    /**
     * 查询二九日报列表
     */
    @PreAuthorize("@ss.hasPermi('pk:ej_daily_report:list')")
    @GetMapping("/list")
    public TableDataInfo list(PkEjDailyReport pkEjDailyReport)
    {
        startPage();
        SysUser user = userMapper.selectUserById(SecurityUtils.getLoginUser().getUser().getUserId());
        String ejDj = user.getEjDj();//问题等级
        String bz = user.getRemark2();//管辖区域
        pkEjDailyReport.setYzcd(ejDj);//用来mybatis判断该字段是否为空，以便知道是否执行对应的条件
        pkEjDailyReport.setYzcdList(ejDj.split("、"));//问题等级条件
        pkEjDailyReport.setBz(bz);//用来mybatis判断该字段是否为空，以便知道是否执行对应的条件
        pkEjDailyReport.setBzList(bz.split("、"));//管辖区域条件
        List<PkEjDailyReport> pkZlDailyReportList = pkEjDailyReportService.selectPkEjDailyReportList(pkEjDailyReport);
        List<PkEjDailyReport> list = new ArrayList<>(pkZlDailyReportList);
        //根据角色判断可以查看哪些流程节点信息，流程（不可越级）：品控员->责任人->品控员->值长->品控主管->品控经理->厂长/主任
        String roleKey = SecurityUtils.getLoginUser().getUser().getRoles().get(0).getRoleKey();//查询当前登录者的角色信息
        if(roleKey.equals("ej_zrr")){ //二九责任人
            list = list.stream().filter(item -> item.getSftj().equals("01") ||
                    item.getSftj().equals("1")|| item.getSftj().equals("2")).collect(Collectors.toList());
        }
        if(roleKey.equals("ej_zz")){ //值长
            list = list.stream().filter(item ->
                    item.getSftj().equals("1")|| item.getSftj().equals("2")).collect(Collectors.toList());
        }
        if(roleKey.equals("ej_pkzg")){ //二九品控主管
            list = list.stream().filter(item ->
                    !("").equals(item.getZzyj()) &&
                    null != item.getZzyj() &&
                    item.getSftj().equals("1")|| item.getSftj().equals("2")).collect(Collectors.toList());
        }
        if(roleKey.equals("ej_pkjl")){ //二九品控经理
            list = list.stream().filter(item ->
                    !("").equals(item.getPkzgyj()) &&
                    null != item.getPkzgyj() &&
                    item.getSftj().equals("1")|| item.getSftj().equals("2")).collect(Collectors.toList());
        }
        if(roleKey.equals("ej_cz_zr")){ //二九厂长/主任
            list = list.stream().filter(item ->
                    !("").equals(item.getPkjlyj()) &&
                    null != item.getPkjlyj() &&
                    item.getSftj().equals("1")|| item.getSftj().equals("2")).collect(Collectors.toList());
        }

        return getDataTable(list);
    }

    /**
     * 导出二九日报列表
     */
    @PreAuthorize("@ss.hasPermi('pk:ej_daily_report:export')")
    @Log(title = "二九日报", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PkEjDailyReport pkEjDailyReport)
    {
        List<PkEjDailyReport> list = pkEjDailyReportService.selectPkEjDailyReportList(pkEjDailyReport);
        ExcelUtil<PkEjDailyReport> util = new ExcelUtil<PkEjDailyReport>(PkEjDailyReport.class);
        return util.exportExcel(list, "二九日报数据");
    }

    /**
     * 获取二九日报详细信息
     */
    @PreAuthorize("@ss.hasPermi('pk:ej_daily_report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(pkEjDailyReportService.selectPkEjDailyReportById(id));
    }

    /**
     * 新增二九日报
     */
    @PreAuthorize("@ss.hasPermi('pk:ej_daily_report:add')")
    @Log(title = "二九日报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PkEjDailyReport pkEjDailyReport)
    {
        return toAjax(pkEjDailyReportService.insertPkEjDailyReport(pkEjDailyReport));
    }

    /**
     * 修改二九日报
     */
    //@PreAuthorize("@ss.hasPermi('pk:ej_daily_report:edit')")
    @Log(title = "二九日报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PkEjDailyReport pkEjDailyReport)
    {
        return toAjax(pkEjDailyReportService.updatePkEjDailyReport(pkEjDailyReport));
    }

    /**
     * 删除二九日报
     */
    @PreAuthorize("@ss.hasPermi('pk:ej_daily_report:remove')")
    @Log(title = "二九日报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pkEjDailyReportService.deletePkEjDailyReportByIds(ids));
    }

    /**
     * 周报-班组问题数量统计
     */
    @GetMapping("/getListByBzSl")
    public TableDataInfo getListByBzSl(PkEjDailyReport pkEjDailyReport) {
        List<PkEjDailyReport> list = pkEjDailyReportService.getListByBzSl(pkEjDailyReport);
        return getDataTable(list);
    }

    /**
     * 周报-各问题类别数量
     */
    @GetMapping("/getListByWtlb")
    public TableDataInfo getListByWtlb(PkEjDailyReport pkEjDailyReport) {
        List<PkEjDailyReport> list = pkEjDailyReportService.getListByWtlb(pkEjDailyReport);
        return getDataTable(list);
    }

    /**
     * 周报-各班组问题等级数量统计
     */
    @GetMapping("/getListByYzcd")
    public TableDataInfo getListByYzcd(PkEjDailyReport pkEjDailyReport) {

        List<PkEjDailyReport> list = pkEjDailyReportService.getListByYzcd(pkEjDailyReport);
        return getDataTable(list);
    }

    /**
     * 月报
     * 班组问题数量统计
     */
    @GetMapping("/getListByBzY")
    public TableDataInfo getListByBzY(PkEjDailyReport pkEjDailyReport) {
        List<PkEjDailyReport> list = pkEjDailyReportService.getListByBzY(pkEjDailyReport);
        return getDataTable(list);
    }

    /**
     * 周报
     * 各问题类别数量
     */
    @GetMapping("/getListByWtlbY")
    public TableDataInfo getListByWtlbY(PkEjDailyReport pkEjDailyReport) {
        List<PkEjDailyReport> list = pkEjDailyReportService.getListByWtlbY(pkEjDailyReport);
        return getDataTable(list);
    }

    /**
     * 周报
     * 各班组问题等级数量统计
     */
    @GetMapping("/getListByYzcdY")
    public TableDataInfo getListByYzcdY(PkEjDailyReport pkEjDailyReport) {
        List<PkEjDailyReport> list = pkEjDailyReportService.getListByYzcdY(pkEjDailyReport);
        return getDataTable(list);
    }

    /**
     * 查询二九已处理完毕、未处理数量
     */
    @GetMapping("/getEjSl")
    public Map<String,Long> getEjSl(PkEjDailyReport pkEjDailyReport) {
        Long ycl = pkEjDailyReportService.selectPkEjDailyReportList(pkEjDailyReport).stream().filter(item -> item.getSftj().equals("2")).count();
        Long dcl = pkEjDailyReportService.selectPkEjDailyReportList(pkEjDailyReport).stream().filter(item -> !item.getSftj().equals("2")).count();
        Map<String,Long> map = new HashMap<>();
        map.put("ej_ycl",ycl);
        map.put("ej_dcl",dcl);
        return map;
    }
}
